package com.jiangxiaofei.citycloud.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.village.VillageInfoPublishActivity;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.List;

/**
 * Created by JiangXFei on 2017/8/21.
 */

public class VillagePublishHeaderPhotoAdapter extends RecyclerView.Adapter<VillagePublishHeaderPhotoAdapter.MyViewHolder> {
    private Context mContext;
    private List<ImageItem> imgList;
    private Handler handler;

    public VillagePublishHeaderPhotoAdapter(Context mContext, List<ImageItem> imgList, Handler handler) {
        this.mContext = mContext;
        this.imgList = imgList;
        this.handler = handler;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_village_publish_photo, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Glide.with(mContext).load(imgList.get(position).path).into(holder.imgPhoto);
        if (position == 0) {
            holder.tvSetPage.setVisibility(View.VISIBLE);
        } else {
            holder.tvSetPage.setVisibility(View.GONE);
        }
        holder.imgClose.setVisibility(View.VISIBLE);
        holder.imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgList.remove(position);
                notifyDataSetChanged();
                if (imgList.size() == 0) {
                    Message msg = new Message();
                    msg.what = VillageInfoPublishActivity.SHOWADDPHOTO;
                    msg.obj=position;
                    handler.sendMessage(msg);
                }else{
                    Message msg = new Message();
                    msg.what = VillageInfoPublishActivity.HIDEADDPHOTO;
                    msg.obj=position;
                    handler.sendMessage(msg);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return imgList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imgPhoto, imgClose;
        TextView tvSetPage;

        public MyViewHolder(View itemView) {
            super(itemView);
            imgClose = itemView.findViewById(R.id.item_publish_img_photo_colse);
            imgPhoto = itemView.findViewById(R.id.item_publish_img_photo);
            tvSetPage = itemView.findViewById(R.id.item_publish_tv_set_page);
        }
    }
}
